home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 581 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  4.4 KB

  1. Path: chronicle.mti.sgi.com!austern
  2. From: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: const_cast
  5. Date: 28 Feb 1996 09:34:09 PST
  6. Organization: Comp Sci, University of Melbourne
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <4h1f1s$c0f@mulga.cs.mu.OZ.AU>
  9. References: <4gq4g3$k9g@jake.esu.edu>
  10. NNTP-Posting-Host: isolde.mti.sgi.com
  11. X-Original-Date: 28 Feb 1996 11:41:48 GMT
  12. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  13.     iQBVAwUBMTSSHky4NqrwXLNJAQEX6gIAvXm60thj9Ic0WzfVYuQclItPs7hLhpqy
  14.     Cfdv+JrwTUNoDFfzPH5zjWyBsxTOavJ3a0zRisy8Uuw6+sl9kM/jNg==
  15.     =Vs/C
  16. Originator: austern@isolde.mti.sgi.com
  17.  
  18. jpotter@falcon.lhup.edu (John E. Potter) writes:
  19.  
  20. >The following produced interesting results on several compilers.  I am not
  21. >using the compilers to interpret the DWP, but am trying to use the DWP to
  22. >justify the compilers and predict the future.  None of these compilers
  23. >support the four new specific casts; however, I will try to explain their
  24. >actions in terms of them.  The question involves the first item following
  25. >return in fun.  I believe the += disambiguates to an expression statement.
  26. >
  27. >struct I1 {
  28. >    I1 (int v) : x(v) { }
  29. >    I1& operator+= (const I1& rhs) { x += rhs.x; return *this; }
  30. >    int x;
  31. >    };
  32. >struct I2 {
  33. >    I2 (int v) : x(v) { }
  34. >    I2 (const I2& src) : x(src.x) { }
  35. >    I2& operator+= (const I2& rhs) { x += rhs.x; return *this; }
  36. >    int x;
  37. >    };
  38. >int nofun (const int& s) { return (int&)(s) += 10; }
  39. >int fun (const int& s) { return int(s) += 10; }
  40. >I1 fun (const I1& s) { I1 t(10); return I1(s) += t; }
  41. >I2 fun (const I2& s) { I2 t(10); return I2(s) += t; }
  42. >
  43. >          comp1            comp2            comp3            comp4
  44. >nofun     const_cast<int&> const_cast<int&> const_cast<int&> const_cast<int&>
  45.  
  46. The compilers are all correct here.
  47.  
  48. >fun(int)  const_cast<int&> static_cast<int> static_cast<int> static_cast<int>
  49. >          *****            error lvalue     error lvalue     error lvalue
  50.  
  51. Compiler `comp1' is wrong.  The other compilers are correct.
  52. The invocation of `+=' is illegal here, because 5.17[expr.ass]/1
  53. requires that the left-hand operand be an lvalue, and `int(s)' is not
  54. an lvalue.
  55.  
  56. >fun(I1)   static_cast<I1>  const_cast<I1&>  static_cast<I1>  static_cast<I1>
  57. >          no error         *****            no error         error lvalue
  58.  
  59. Compiler `comp2' is wrong.  The other compilers are correct.
  60.  
  61. 5.17[expr.ass]/1 does not apply in this case, because `+=' here
  62. refers to an overloaded operator, not the built-in version.
  63. In this case, `+=' is a member function, and it is OK for the
  64. left-hand operand to be a non-lvalue.
  65.  
  66. Some people might argue that 12.2[class.temporary] allows the behaviour
  67. of `comp2', but if that is true, it is a problem in the wording of 12.2 -
  68. I think the intent is definitely that `I1(s)' should be required to make
  69. a copy of `s'.
  70.  
  71. >fun(I2)   static_cast<I2>  static_cast<I2>  static_cast<I2>  static_cast<I2>
  72. >       no error         no error         no error         no error
  73.  
  74. Again, no problems here -- all the compilers are correct.
  75.  
  76. >When I originally wrote fun, my intent was to create an unnamed temp and
  77. >return a modification of it.  This requires the use of a copy constructor
  78. >and I was surprised to find that I got different results when I wrote it
  79. >and when I let the compiler implicitly provide it.
  80.  
  81. As I said above, I think this is a bug in `comp2'.
  82.  
  83. >fun(I2) gave me what
  84. >I wanted and has been permitted by many compilers.  If I read the DWP
  85. >correctly, constructors return rvalues and it is now illegal.
  86.  
  87. You don't read it correctly.  Constructors return rvalues, but a call
  88. to a member function does not require an lvalue, even if that member
  89. function is `operator +='.
  90.  
  91. >Does an explicitly written copy constructor with
  92. >the same semantics as the implicit one have different meaning?
  93.  
  94. No.
  95.  
  96. --
  97. Any mail to me between Feb 24th and 27th may have gone to the bit bucket
  98. (#&$^@$!# mail software silently ignored disk full error).  Please resend.
  99. (comp.std.c++ articles were not affected, so no need to resend those.)
  100. Fergus Henderson                 WWW: http://www.cs.mu.oz.au/~fjh
  101. fjh@cs.mu.oz.au                  PGP: finger fjh@128.250.37.3
  102. ---
  103. [ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
  104.                 newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  105.   comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  106.   Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  107.   Comments? mailto:std-c++-request@ncar.ucar.edu 
  108. ]
  109.